<?php
defined( '_VALID_MOS' ) or die('<script>window.location.href="http://www.kinifurniture.com";</script>');

$adm_functions->is_admin_login();
$section='Edit Gallery';
$adm_menu='
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
	<tr>
		<td class="menudottedline" width="40%">
			<div class="pathway"><img src="assets/admin/'.$SITE_LOGO_ADMIN.'"/></div>	</td>
		<td class="menudottedline" align="right">
			<table cellpadding="0" cellspacing="0" border="0" id="toolbar">
				<tr valign="middle" align="center">
					<td>&nbsp;</td>
					<td>
					<a class="toolbar" href="javascript:kliksubmit();">
						<img src="show_files/save.png"  alt="Save" name="save" title="Save" align="middle" border="0" />				<br />Save</a>
					</td>
					<td>&nbsp;</td>
					<td>
						<a class="toolbar" href="admin.php?show=gallery_manager&coll='.$coll.'">
						<img src="show_files/cancel.png"  alt="Cancel" name="cancel" title="Cancel" align="middle" border="0" />				<br />Cancel</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<br />
';

if ($id)
{
	$qryedit="select * from albumdetail where id_gambar=$id";
	$selectedit=$dbh->query($qryedit);

	while($sqlRowedit=mysql_fetch_object($selectedit))
	{
		$val_id_gallery=$sqlRowedit->id_gambar;
		$val_id_album=$sqlRowedit->id_album;
		$val_title=$sqlRowedit->nama_gambar;
		$val_description=$sqlRowedit->deskripsi;
		$val_img_gallery=$sqlRowedit->image;
		if ($val_img_gallery=='') $val_img_gallery='noimg.png';
		if ($val_img_gallery=='' || $val_img_gallery=='noimg.png')
			$image='<input type="file" name="inp_upl_img"/>';
		else
			$image='<input class="inputbox" type="checkbox" name="inp_del_img" value="Y" /> Delete this image <font color="green">('.$val_img_gallery.')</font>';
	}
}

if($inp_title)
{
	if ($inp_del_img=='Y')
	{
		$dbh->query("update albumdetail set 
			nama_gambar='$inp_title', 
			image='',
			deskripsi='$inp_description'
			where id_gambar=$inp_id");
		@unlink("assets/albumdetail/".$inp_image);
		echo '<script>window.location.href="admin.php?show=gallery_manager&sort='.$inp_idalbum.'";</script>';

	} else
	{
		if($_FILES['inp_upl_img']['name'])
		{
			$imageexists='N';
			$img_name=basename($_FILES['inp_upl_img']['name']);
			$qryname="select * from albumdetail where image='$img_name'";
			$selectname=$dbh->query($qryname);
			while($sqlRowedit=mysql_fetch_object($selectname))
			{
				$imageexists='Y';
			}
			if ($imageexists=='Y') 
			{
				echo '<script>alert("The Filename is already exists, try another filename");</script>';
			}
			else
			{
				$uploaddir = 'assets/albumdetail/';
				$uploadfile = $uploaddir . basename($_FILES['inp_upl_img']['name']);
				move_uploaded_file($_FILES['inp_upl_img']['tmp_name'], $uploadfile);
				$img_name=basename($_FILES['inp_upl_img']['name']);
				$qry_image='image=\''.$img_name.'\',';
			}
		} else 	$qry_image='';

		if ($imageexists!='Y')
		{
			$dbh->query("update albumdetail set 
				nama_gambar='$inp_title',
				$qry_image
				deskripsi='$inp_description'
				where id_gambar=$inp_id");
			echo '<script>window.location.href="admin.php?show=gallery_manager&coll='.$inp_idalbum.'";</script>';
		}
	}
}
?>